package com.uzykj.modules.blog.service;

import com.google.common.collect.Lists;
import com.uzykj.core.persistence.Page;
import com.uzykj.core.service.CrudService;
import com.uzykj.modules.blog.entity.BlogCategory;
import com.uzykj.modules.blog.entity.BlogNote;
import com.uzykj.modules.blog.mapper.BlogNoteMapper;
import com.uzykj.modules.blog.utils.NoteUtil;
import com.uzykj.modules.sys.entity.User;
import com.uzykj.modules.sys.utils.UserUtils;
import freemarker.template.utility.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ghostxbh
 * @date 2020/7/8
 * @description
 */
@Service
@Transactional(readOnly = true)
public class BlogNoteService extends CrudService<BlogNoteMapper, BlogNote> {

    @Autowired
    private BlogLabelService blogLabelService;

    public List<BlogNote> getMostPopular(BlogNote blogNote) {
        return mapper.findFuzzyList(blogNote);
    }

    public BlogNote getByCode(String code) {
        BlogNote note = mapper.getByCode(code);
        note.setCreateBy(UserUtils.get(note.getCreateBy().getId()));
        return note;
    }

    public BlogNote getMaxCode() {
        String maxCode = mapper.getMaxCode();
        String code = NoteUtil.getCode(maxCode);
        BlogNote blogNote = new BlogNote();
        blogNote.setCode(code);
        return blogNote;
    }

    public boolean isExistCode(String code, User createBy) {
        BlogNote note = new BlogNote();
        note.setCode(code);
        note.setCreateBy(createBy);
        BlogNote blogNote = mapper.get(note);
        if (blogNote != null) {
            return true;
        }
        return false;
    }

    public long count(BlogNote note) {
        return mapper.count(note);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(BlogNote entity) {
        if (entity.getIsNewRecord()) {
            entity.preInsert();
            mapper.insert(entity);
            // 分类关联
            mapper.insertNoteCategory(entity);
            // 处理标签
            String labels = entity.getLabels();
            blogLabelService.processLabels(labels, null);
        } else {
            entity.preUpdate();
            mapper.update(entity);
            // 分类关联
            BlogNote blogNote = mapper.get(entity.getId());
            BlogCategory category = entity.getCategory();
            BlogCategory blogCategory = blogNote.getCategory();
            if (category != null && blogCategory != null) {
                if (!(category.getId().equals(blogCategory.getId()))) {
                    mapper.deleteNoteCategory(blogNote);
                    mapper.insertNoteCategory(entity);
                }
            } else mapper.insertNoteCategory(entity);
            // 处理标签
            String newLabels = entity.getLabels();
            String oldLabels = blogNote.getLabels();
            blogLabelService.processLabels(newLabels, oldLabels);
        }
    }

    /**
     * 查询分页数据
     *
     * @param page   分页对象
     * @param entity
     * @return
     */
    @Override
    public Page<BlogNote> findPage(Page<BlogNote> page, BlogNote entity) {
        User createBy = entity.getCreateBy();
        dataRuleFilter(entity);
        entity.setPage(page);
        if (createBy != null) {
            entity.setCreateBy(createBy);
        } else if (!StringUtils.isEmpty(entity.getLabels())) {
            return page.setList(mapper.getByLabel(entity));
        } else if (!StringUtils.isEmpty(entity.getCode())) {
            return page.setList(mapper.blurCode(entity));
        }
        page.setList(mapper.findList(entity));
        return page;
    }

    public Page<BlogNote> aggregate(Page<BlogNote> page, BlogNote entity) {
        User createBy = entity.getCreateBy();
        dataRuleFilter(entity);
        entity.setPage(page);
        List<BlogNote> allNote = new ArrayList<BlogNote>();
        if (createBy != null) {
            BlogNote note = new BlogNote();
            note.setCreateBy(createBy);
            List<BlogNote> list = mapper.findList(note);
            allNote.addAll(list);
        }

        if (entity.getLabels() != null) {
            BlogNote note = new BlogNote();
            note.setLabels(entity.getLabels());
            List<BlogNote> byLabel = mapper.getByLabel(note);
            allNote.addAll(byLabel);
        }

        if (entity.getCode() != null) {
            return page.setList(mapper.blurCode(entity));
        }
        page.setList(mapper.findList(entity));
        return page;
    }

    public List<User> topAuthor() {
        List<String> anthors = mapper.findTopAnthor(1);
        return Optional.ofNullable(anthors)
                .orElse(Lists.newArrayList())
                .stream()
                .map(author -> UserUtils.get(author))
                .collect(Collectors.toList());
    }

    @Transactional(readOnly = false)
    public void updateRead(String id, int readCount) {
        mapper.updateRead(id, readCount);
    }

    @Transactional(readOnly = false)
    public void updateLike(String id) {
        mapper.updateLike(id);
    }
}


